﻿using System.Collections.Generic;
using System.Reflection;
using System.Linq;

namespace FluentResourceMapping.AutoMapping
{
    public class AutoTypeResourceMapping<T> : TypeResourceMap<T>
    {
        public IAutomappingConfiguration AutomappingConfiguration { get; private set; }

        public IList<MemberInfo> MappedMembers { get; private set; }

        public AutoTypeResourceMapping(IAutomappingConfiguration automappingConfiguration,
                                       IMemberResourceMappingConvention mappingConvention)
            : base()
        {
            AutomappingConfiguration = automappingConfiguration;
            MappedMembers = new List<MemberInfo>();
            //Mapping = new TypeResourceMapping(typeof(T));

            foreach (MemberInfo member in typeof (T).GetMembers().Where(automappingConfiguration.ShouldMap))
            {
                MappedMembers.Add(member);
                MemberInfo mappedMember = mappingConvention.MapsTo(member);

                if (mappedMember != null)
                    Mapping.MemberResourceMappings.Add(new MemberResourceMapping(member, mappedMember.DeclaringType,
                                                                                 mappedMember.Name));
            }
        }

        public override TypeResourceMapping GetResourceMapping()
        {
            return Mapping;
        }
    }
}